﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Utility;

namespace CookingPlan
{
    class IngredientsReport
    {
        private static readonly XmlDocument Report;

        /// <summary>
        /// 初始化
        /// </summary>
        static IngredientsReport()
        {
            Report = new XmlDocument();
            Report.LoadXml("<?xml version=\"1.0\"?>" +
                            "<html><head>" +
                            "<meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"utf-8\" />" +
                            "<title>Cooking Plan-Ingredients Report</title>" +
                            "<style type=\"text/css\">" +
                            "body{background-color:#cccccc}" +
                            ".alias{width:300px;text-align:left;font-size:16px;}" +
                            ".single{background-color:#99ffff}" +
                            ".double{background-color:#ffbbff}" +
                            "</style></head><body>" +
                            "<table style=\"margin-left:auto;margin-right:auto;text-align:center;border:2\" border=\"1\">" +
                            "<tr style=\"background-color:#00ff00\">" +
                            "<td style=\"width:100px;color:red;\">Container NO.</td>" +
                            "<td style=\"width:200px;color:red;\">Ingredient Name</td>" +
                            "<td style=\"width:200px;color:red;\">Ingredient Amount</td>" +
                            "<td style=\"width:300px;color:red;\">Ingredient Alias</td>" +
                            "</tr></table></body></html>");
        }

        /// <summary>
        /// 保存食材报告文件
        /// </summary>
        public static void GetReportFile(IEnumerable<Ingredient> ingredients, string path)
        {
            SetTable(ingredients);
            string html = Report.GetElementsByTagName("html")[0].OuterXml;
            FileStream fs = File.OpenWrite(path + "report.html");
            byte[] bs = Encoding.UTF8.GetBytes(html);
            fs.Write(bs, 0, bs.Length);
            fs.Close();
        }

        private static void SetTable(IEnumerable<Ingredient> ingredients)
        {
            XmlNode table = Report.GetElementsByTagName("table")[0];
            int index = 1;
            foreach(Ingredient i in ingredients)
            {
                XmlElement tr = Report.CreateElement("tr");
                tr.SetAttribute("class", index % 2 == 0 ? "double" : "single");

                XmlElement no = Report.CreateElement("td");
                XmlText noText = Report.CreateTextNode(index.ToString());
                no.AppendChild(noText);
                tr.AppendChild(no);

                XmlElement name = Report.CreateElement("td");
                name.SetAttribute("style", "color:blue");
                XmlText nameText = Report.CreateTextNode(i.Name);
                name.AppendChild(nameText);
                tr.AppendChild(name);

                XmlElement amount = Report.CreateElement("td");
                amount.SetAttribute("style", "color:blue");
                XmlText amountText = Report.CreateTextNode(i.Amount + (i.Dimension == Dimension.g ? "g" : "ml"));
                amount.AppendChild(amountText);
                tr.AppendChild(amount);

                XmlElement alias = Report.CreateElement("td");
                alias.SetAttribute("class", "alias");
                XmlText aliasText = Report.CreateTextNode("");//TODO 需要调用编译模块来判断
                alias.AppendChild(aliasText);
                tr.AppendChild(alias);

                table.AppendChild(tr);
                index++;
            }
        }
    }
}
